<?php

/**
 * Ein Wrapper fuer die PEAR Klasse HTML_TreeMenu
 *
 *
 * ACHTUNG !!!
 * Zeile 432 von TreeMenu.js wurde entsprechend angepasst:
 * document.cookie = 'TreeMenuBranchStatus=' + cookieString.join(':') + ';path=/';
 *
 */

require_once(WSB_MODULES_PATH . 'media/Url.php');

class WSB_Dhtml_FolderTree
{

	const MODE_MEDIA	= 0;
	const MODE_TRASH	= 1;
	const MODE_UPLOAD	= 2;

	/**
	 * @var integer
	 */
	private $_mode;

	/**
	 * @var string
	 */
	private $_root;

	/**
	 * @var string
	 */
	private $_path;

	/**
	 * @param string $root
	 * @param integer $mode
	 */
	public function __construct($root, $mode=null)
	{
		$this->_root = $root;
		$this->_mode = isset($mode) ? $mode : self::MODE_MEDIA ;
	}

	/**
	 * @return void
	 */
	public function render()
	{
    	$treeMenu = $this->getMenu();
    	echo '<script src="../WSB/library/PEAR/HTML/TreeMenu/TreeMenu.js" language="JavaScript" type="text/javascript"></script>';
    	$treeMenu->printMenu();
	}

	/**
	 * @param string $root
	 */
	public function setRoot($root)
	{
		$this->_root = $root;
	}

	/**
	 * @param string $path
	 */
	public function setPath($path)
	{
		$this->_path = $path;
	}

	/**
	 * @return HTML_TreeMenu_DHTML
	 */
	public function getMenu()
	{

		$dir = new WSB_DirectoryIterator($this->_root);

    	$menu  = new HTML_TreeMenu();
		$treenode = new HTML_TreeNode(
			array(
				'text' => $dir->getFilename(),
				'link' => $this->_getLink($dir),
				'icon' => 'folder.gif',
				'expandedIcon' => 'folder-expanded.gif',
				//'expanded' => true,
				'cssClass' => ($dir->getPathname() == $this->_path) ? 'treeMenuDefaultCurrent' : 'treeMenuDefault'
			)
		);
		$this->_walkThrough($dir, $treenode);
		$menu->addItem($treenode);
		return new HTML_TreeMenu_DHTML($menu,
			array(
				//'images' => '../WSB/library/PEAR/HTML/TreeMenu/imagesAlt',
				'images' => '../WSB/design/default/skin/default/icon/famfam',
				'defaultClass' => 'treeMenuDefault'
			)
		);
	}

	/**
	 * @param WSB_DirectoryIterator $dir
	 * @param HTML_TreeNode $treenode
	 */
	private function _walkThrough(WSB_DirectoryIterator $dir, HTML_TreeNode $treenode)
	{
		foreach($dir AS $fileinfo) {
			if(!$fileinfo->isDot() && $fileinfo->isDir()) {
				$childtreenode = &$treenode->addItem(
					new HTML_TreeNode(array(
						'text' => $fileinfo->getFilename(),
						'link' => $this->_getLink($fileinfo),
						'icon' => 'folder.gif',
						'expandedIcon' => 'folder-expanded.gif',
						'cssClass' => ($fileinfo->getPathname() == $this->_path) ? 'treeMenuDefaultCurrent' : 'treeMenuDefault'
					))
				);
				if(!$fileinfo->isEmpty()) {
					$path = $fileinfo->getPathname();
					$dir = new WSB_DirectoryIterator($path);
					$this->_walkThrough($dir, $childtreenode);
				}
			}
		}
	}

	/**
	 * @param WSB_DirectoryIterator $filepath
	 * @return string
	 */
	private function _getLink(WSB_DirectoryIterator $filepath)
	{
		$link = '';
		switch($this->_mode) {
			case self::MODE_MEDIA:
				$path = $filepath->getRelativeMediaPath();
				$link = Media_Url::fileListUrl($path);
				break;
			case self::MODE_TRASH:
				$path = $filepath->getRelativeTrashPath();
				$link = Media_Url::trashListUrl($path);
				break;
			case self::MODE_UPLOAD:
				$path = $filepath->getRelativeMediaPath();
				$link = Media_Url::uploadListUrl($path);
				break;
		}
		return $link;
	}

}
